import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // build用来构建页面
  @override
  Widget build(BuildContext context) {
    // MaterialApp 通过它可以设置应⽤的名称、主题、语⾔、⾸⻚及路由 列表等
    return MaterialApp(
      //应⽤名称
      title: 'Flutter Demo',
      theme: ThemeData(
        //蓝⾊主题
        primarySwatch: Colors.green,
      ),
      // 注册路由表
      routes: {
        'new_page': (context) => NewRoute(),
        'tip_widgets': (context) => EchoRoute("注册表路由页面內容固定,不能动态传值"),
      },
      // 应⽤⾸⻚路由
      home: MyHomePage(title: 'Flutter Demo 首页 测试'),
    );
  }
}

// 路由1
class NewRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('new route'),
      ),
      body: Center(
        child: Text('这是一个新的页面'),
      ),
    );
  }
}

// 路由2
class EchoRoute extends StatelessWidget {
  EchoRoute(this.tip);
  final String tip;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('注册表路由页面'),
      ),
      body: Center(
        child: Text(this.tip),
      ),
    );
  }
}

// ⽣成随机字符串
class RandomWordsWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // ⽣成随机字符串
    final wordPair = new WordPair.random();
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Text(wordPair.toString()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            RandomWordsWidget(),
            FlatButton(
              child: Text('跳转页面'),
              textColor: Colors.blue,
              onPressed: () {
                //导航到新路由
                Navigator.pushNamed(context, "new_page");
                // Navigator.push(context,
                //     new MaterialPageRoute(builder: (context) {
                //   return new NewRoute();
                // }));
              },
            ),
            OutlineButton(
              child: Text('路由2'),
              textColor: Colors.green,
              onPressed: () {
                Navigator.pushNamed(context, 'tip_widgets');
              },
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
